package ru.mcfr.oxygen.updater;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static java.lang.System.*;


/**
 * Created by IntelliJ IDEA.
 * User: wstarcev
 * Date: 15.04.11
 * Time: 23:10
 * To change this template use File | Settings | File Templates.
 */
public class Main {

    public static void main(String[] arg){

        String path_to = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String baseDir = null;
        try {
            baseDir = URLDecoder.decode(new File(path_to).getParent(), "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

//        if (arg.length == 0)
//            System.out.println(" -thread    to run process in thread");
//
        final UpdateProcessor processor = UpdateProcessor.getInstance(baseDir, baseDir);
        processor.setProperty("server-url","http://altair:8888");
        processor.setProperty("use-proxy", "true");
        processor.setProperty("proxy-server", "proxy");
        processor.setProperty("proxy-user-name", "vsedov");
        processor.setProperty("proxy-user-password", "dzxtckfd");
        processor.save();
        //processor.run();

        //processor.showSettingsDialog();

//        File zipFile = new File(baseDir + File.separator + "arc.zip");
//        Zipper.zip(new File(baseDir + File.separator + "updates"), zipFile.getAbsolutePath());



        //Zipper.unzip(new File("/home/ws/0328dd93-d795-4676-9517-e4f372f0e1aa.zip"), "/home/ws/0328dd93-d795-4676-9517-e4f372f0e1aa");
        //FileInstaller fi = new FileInstaller("");

//        Iterator pr = getProperties().keySet().iterator();
//        while (pr.hasNext()){
//            Object oKey = pr.next();
//            if (oKey instanceof String)
//                out.println((String) oKey + " = " + getProperty((String) oKey));
//        }
//
//        if (arg.length > 0) {
//            if (arg[0].equals("-thread")) {
                (new Thread() {
                    public void run() {
                        boolean doUpdate = true;
                        while (doUpdate) {
                            try {
                                out.println(processor.run());
                                sleep(1000 * 10);// * Integer.valueOf(processor.getProperty("update-per-minutes", "1")));
                            } catch (Exception e) {
                                e.printStackTrace();
                                err.println(e.getLocalizedMessage());
                            }
                        }
                        exit(0);
                    }
                }).run();
//            }
//        }


//        System.out.println("finished");
        exit(0);
    }
}
